
#include <Archaism/Logger.h>
#include <Archaism/String.h>

_BeginNamespace(Radix)

Logger::Logger()
	:mName(),
	mLevel(eLevel_Info),
	mStream()
{}

Logger::~Logger()
{
	this->close();
}

bool Logger::open(const String& name, const Level& lev)
{
	if(mStream.is_open())
		return false;
	mLevel = lev;
	mStream.open(name);
	return mStream.is_open();
}

void Logger::close()
{
	if(mStream.is_open())
	{
		mStream.flush();
		mStream.close();
	}
}

Logger::Level Logger::level() const
{
	return mLevel;
}

void Logger::setLevel(const Level& lev)
{
	mLevel = lev;
}

void Logger::logMessage(const Level& lev, const String::value_type* fmt, ...)
{
	if(lev < mLevel)
		return;

	String message;
	va_list ap = 0;
	va_start(ap, fmt);
	StringUtil::format(message, fmt, ap); 
	va_end(ap);

	this->logMessage(message);
}

void Logger::logMessage(const String& message)
{
	if(! mStream.is_open())
		return;
	mStream <<  StringUtil::toUtf8(message) << std::endl;
}

_EndNamespace(Radix)
